/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src.services.loan;

/**
 *
 * @author jay
 */

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import javax.faces.model.SelectItem;
import src.domain.loan.Loanprod;
import src.services.CRUEDService;
import src.util.Utils;

public class LoanprodModel implements java.io.Serializable {
    
    private CRUEDService service;
    
    public LoanprodModel() {
        service = new CRUEDService();
    }
          
    public List<Loanprod> findAll(Map<String, String> filters) {
        return (List<Loanprod>) service.findAll("Loanprod", filters);
    }
    
    public List<SelectItem> listAll() {
        return service.listAll("Loanprod", "loanprodId","productName");
    }
    
    public BigDecimal getInterestRate(Integer id, BigDecimal amount) {
        return (BigDecimal) service.getObjectSQLdirect("SELECT loan_modul.get_interest_rate("+id+","+amount+") FROM DUAL");
    }
    
    public boolean isProductExist(String value, Integer id) {
        BigDecimal count;
        String cond = " where product_no = "+Utils.fmtSQLString(value);
        if (id != null) cond+=" and loanprod_id <> "+id;
        
        try { count = (BigDecimal) service.getObjectSQLdirect("select count(*) from loanprod "+cond);
        } catch (RuntimeException e) {count = BigDecimal.ZERO;}
        
        return (count.compareTo(BigDecimal.ZERO)==0 ? false : true);
    }
    
    public Loanprod getById(Integer id) {
        return (Loanprod)  service.getById(Loanprod.class, id);
    }
    
    public boolean saveData(Loanprod data) {
        return service.saveData(data);
    }
    
    public boolean deleteData(Loanprod data) {
        return service.deleteData(data);
    }
}
